package com.article.service.impl;

import java.sql.Timestamp;
import java.util.List;

import com.article.dao.IArticleDAO;
import com.article.entity.Article;
import com.article.service.IArticleService;

@SuppressWarnings("unchecked")
public class ArticleServiceImpl implements IArticleService {
	private IArticleDAO articleDAOImpl;

	public boolean addArticle(Article article) {
		try {
			// 设置文章添加时间
			article.setAddTime(new Timestamp(System.currentTimeMillis()));
			// 设置文章点击次数为0
			article.setClickTimes(0);
			articleDAOImpl.insert(article);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean deleteArticle(String id) {
		if (null == id || "".equals(id)) {
			return false;
		}
		try {
			articleDAOImpl.delete(Integer.parseInt(id));
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean editArticle(Article article) {
		try {
			Article newArticle = (Article) articleDAOImpl.find(article.getId());
			newArticle.setContent(article.getContent());
			newArticle.setTitle(article.getTitle());
			articleDAOImpl.update(newArticle);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public List<Article> findAllByPage(String start, String limit) {
		if (start == null || "".equals(start)) {
			start = "0";
		}
		if (limit == null || "".equals(limit)) {
			limit = "5";
		}
		return articleDAOImpl.findAllByPage(Integer.parseInt(start), Integer
				.parseInt(limit));
	}

	public List<Article> findByCateByPage(String cateId, String start,
			String limit) {
		if (cateId == null || "".equals(cateId)) {
			return findAllByPage(start, limit);
		}
		if (start == null || "".equals(start)) {
			start = "0";
		}
		if (limit == null || "".equals(limit)) {
			limit = "5";
		}
		return articleDAOImpl.findByCategory(Integer.parseInt(cateId), Integer
				.parseInt(start), Integer.parseInt(limit));
	}

	public List<Article> findByKeyWordsByPage(String keyWords, String start,
			String limit) {
		if (keyWords == null || "".equals(keyWords)) {
			return findAllByPage(start, limit);
		}
		if (start == null || "".equals(start)) {
			start = "0";
		}
		if (limit == null || "".equals(limit)) {
			limit = "5";
		}
		return articleDAOImpl.findByTitle(keyWords, Integer.parseInt(start),
				Integer.parseInt(limit));
	}

	public int findAllCount() {
		return ((Long) articleDAOImpl.findAllCount()).intValue();
	}

	public int findCountByCate(String cateId) {
		if (cateId == null || "".equals(cateId)) {
			return 0;
		}
		return ((Long) articleDAOImpl.findCountByCategory(Integer
				.parseInt(cateId))).intValue();
	}

	public int findCountByKeyWords(String keyWords) {
		if (keyWords == null || "".equals(keyWords)) {
			return 0;
		}
		return ((Long) articleDAOImpl.findCountByTitle(keyWords)).intValue();
	}

	public void setArticleDAOImpl(IArticleDAO articleDAOImpl) {
		this.articleDAOImpl = articleDAOImpl;
	}
}
